package com.onemile.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public final class HttpsClient
{
	private final static int READ_TIMEOUT = 15000;
	
	public static String uploadData(String strUrl, byte[] data) throws IOException
	{
		StringBuffer buffer = new StringBuffer();
		HttpsURLConnection conn = null;
		OutputStream out = null;
		InputStream in = null;
		BufferedReader rd = null;
		try
		{
			// ���� SSL
			CertTrustManager.allowAllSSL();
			
			URL url = new URL(strUrl);
			conn = (HttpsURLConnection) url.openConnection();
			conn.setRequestProperty("Accept", "text/xml;text/html;*/*");
			conn.setRequestProperty("Content-Type", "text/xml;charset=utf-8");
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setDefaultUseCaches(false);
			conn.setReadTimeout(READ_TIMEOUT);
			
			// �������
			if (data != null)
			{
				out = conn.getOutputStream();
				out.write(data);
				out.flush();
			}
			
			// ��ȡ���
			String line;
			in = conn.getInputStream();
			rd = new BufferedReader(new InputStreamReader(in, "utf-8"));
			while ((line = rd.readLine()) != null)
			{
				buffer.append(line);
			}
			rd.close();
			
			return buffer.toString().trim();
		}
		catch (IOException ex)
		{
			throw ex;
		}
		catch (Exception ex)
		{
			throw new IOException(ex.getMessage());
		}
		finally
		{
			if (out != null)
			{
				out.close();
				out = null;
			}
			
			if (in != null)
			{
				in.close();
				in = null;
			}
			
			if (conn != null)
			{
				conn.disconnect();
			}
		}
	}
	
	public static class CertTrustManager implements X509TrustManager
	{
		
		private static TrustManager[] _trustManagers;
		private static final X509Certificate[] _acceptedIssuers;
		
		static
		{
			_acceptedIssuers = new X509Certificate[] {};
		}
		
		@Override
		public void checkClientTrusted(X509Certificate[] chain, String authType)
				throws CertificateException
		{
			
		}
		
		@Override
		public void checkServerTrusted(X509Certificate[] chain, String authType)
				throws CertificateException
		{
			
		}
		
		@Override
		public X509Certificate[] getAcceptedIssuers()
		{
			return _acceptedIssuers;
		}
		
		public static void allowAllSSL() throws Exception
		{
			if (_trustManagers == null)
			{
				HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
					@Override
					public boolean verify(String hostname, SSLSession session)
					{
						return true;
					}
				});
				
				_trustManagers = new TrustManager[] { new CertTrustManager() };
				
				SSLContext context = null;
				try
				{
					context = SSLContext.getInstance("TLS");
					context.init(null, _trustManagers, new SecureRandom());
				}
				catch (Exception e)
				{
					throw e;
				}
				
				javax.net.ssl.SSLSocketFactory sf = context.getSocketFactory();
				HttpsURLConnection.setDefaultSSLSocketFactory(sf);
			}
		}
	}
}
